using System;
using System.Collections.Generic;
using System.Linq;
using Granite.DataAnnotations;
using Granite.Modeling;

namespace Rifts.Models.Characters
{
	public class Ability : EditableObjectModelBase
	{
		public Ability(System.Xml.Linq.XElement abilityNode, Random random, string source)
		{
			Name = abilityNode.GetAttribute("Name");
			BaseScore = abilityNode.ParseAttribute("Value", random);
			PerLevel = abilityNode.GetInt2("PerLevel");
			Type = abilityNode.GetEnum<AbilityType>("Type", AbilityType.General);
			Source = source;
		}

		public Ability()
		{
			
		}

		public string Name
		{
			get { return Get<String>("Name"); }
			set { Set(value, "Name"); }
		}

		public AbilityType Type
		{
			get { return Get<AbilityType>("Type"); }
			set { Set(value, "Type"); }
		}

		/// <summary>
		/// The book/page the ability came from
		/// </summary>
		public string Reference
		{
			get { return Get<String>("Reference"); }
			set { Set(value, "Reference"); }
		}

		/// <summary>
		/// This skill, race, or class that granted the ability
		/// </summary>
		public string Source
		{
			get { return Get<String>("Source"); }
			set { Set(value, "Source"); }
		}

		public int? BaseScore
		{
			get { return Get<int?>("BaseScore"); }
			set { Set(value, "BaseScore"); }
		}

		public int? PerLevel
		{
			get { return Get<int?>("PerLevel"); }
			set { Set(value, "PerLevel"); }
		}

		public int Level
		{
			get { return Get<int>(1, "Level"); }
			set { Set(value, "Level"); }
		}

		[CalculatedField("BaseScore,PerLevel,Level")]
		public int? Score
		{
			get { return BaseScore + (PerLevel ?? 0 * (Level - 1)); }
		}

		/// <summary>
		/// The book/page the skill came from
		/// </summary>
		public string BookReference
		{
			get { return Get<String>("BookReference"); }
			set { Set(value, "BookReference"); }
		}


		public Ability Clone()
		{
			var copy = new Ability();
			copy.Name=Name;
			copy.Reference=Reference;
			copy.Source = Source;
			copy.BaseScore = BaseScore;
			copy.PerLevel = PerLevel;
			copy.Level = Level;
			copy.BookReference = BookReference;

			return copy;
		}

	}
}
